#include <vector>
#include <stdio.h>
#include <unistd.h>
#include <iostream>

#include "managerdao_file_impl.h"
#include "manager.h"

using namespace std;

vector<Manager*>& ManagerDaoFileImpl::load(vector<Manager*>& managers)
{
	FILE* frp = fopen("managers.dat","r");
	if(frp == NULL)
	{
		return managers;
	}
	managers.clear();

	int ret = 1;
	while(ret)
	{	
		Manager* manager = new Manager;
		ret = fread(manager,sizeof(Manager),1,frp);

		if(ret)	managers.push_back(manager);
	}

	fclose(frp);
	return managers;
}

void ManagerDaoFileImpl::save(vector<Manager*>& managers)
{
	FILE* fwp = fopen("managers.dat","wb");
	if(fwp == NULL)
	{
		perror("fopen");
		return;
	}
	
	for(unsigned int i=0; i<managers.size(); i++)
	{
		fwrite(managers.at(i),sizeof(Manager),1,fwp);
	}
	
	
	fclose(fwp);

	
}
